﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.MembershipProviders;
using SCRUM.Core.BusinessObjects;
using System.Web.Security;
using SCRUM.Core.DAO;

/// <summary>
/// widget for editing user's own information
/// User can change here their fullname and password
/// Change of password  is successful if: 
///     -oldPassword checks, 
///     -newPassword and reenteredPassword are the same and meet some requirements (the same as during creating user) 
///     -oldPassword and newPassword differs
/// </summary>
public partial class EditUser : WidgetBase
{
    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get
        { 
            return "EditUser"; 
        }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get
        { 
            return (string)GetLocalResourceObject("Title");
        }
    }
    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        u = (User)Membership.GetUser();

        TextBoxFullName.Text = u.FullName;
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {

    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {

    }

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get 
        { 
            return false;
        }
    }

    User u;

    /// <summary>
    /// Clears the form
    /// </summary>
    private void clear()
    {
        TextBoxOldPassword.Text = string.Empty;
        TextBoxNewPassword.Text = string.Empty;
        TextBoxReenterPassword.Text = string.Empty;
    }

    /// <summary>
    /// Edits user's fullname or changes their password
    /// </summary>
    protected void Edit(object sender, EventArgs e)
    {
        LabelResultFullName.Text = string.Empty;
        if (u.FullName != TextBoxFullName.Text && TextBoxFullName.Text.Length > 1)
        {
            if (DAOUser.ChangeFullName(u.Login, TextBoxFullName.Text))
            {
                LabelResultFullName.Text = String.Format((string)GetLocalResourceObject("SuccessFullName"));
                LabelResultFullName.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                LabelResultFullName.Text = String.Format((string)GetLocalResourceObject("FailFullName"));
                LabelResultFullName.ForeColor = System.Drawing.Color.Red;
            }
        }

        LabelResult.Text = string.Empty;

        if (string.IsNullOrEmpty(TextBoxOldPassword.Text))
        {
            clear();
            return;
        }

        if (TextBoxNewPassword.Text != TextBoxReenterPassword.Text)
        {
            LabelResult.Text = String.Format((string)GetLocalResourceObject("Nonidentical"));
            LabelResult.ForeColor = System.Drawing.Color.Red;
            clear();
            return;
        }

        if (TextBoxOldPassword.Text == TextBoxNewPassword.Text)
        {
            LabelResult.Text = String.Format((string)GetLocalResourceObject("Same"));
            LabelResult.ForeColor = System.Drawing.Color.Red;
            clear();
            return;
        }

        if(MembershipProviderFactory.MembershipProvider.ChangePassword(u.Login, TextBoxOldPassword.Text, TextBoxNewPassword.Text))
        {
            LabelResult.Text = String.Format((string)GetLocalResourceObject("Success"));
            LabelResult.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            LabelResult.Text = String.Format((string)GetLocalResourceObject("Fail"));
            LabelResult.ForeColor = System.Drawing.Color.Red;
        }
        clear();
    }
}
